package com.dlyk.service.impl;

import com.dlyk.constant.Constants;
import com.dlyk.mapper.ActivityRemarkMapper;
import com.dlyk.model.ActivityRemark;
import com.dlyk.model.User;
import com.dlyk.query.ActivityRemarkQuery;
import com.dlyk.query.BaseQuery;
import com.dlyk.service.ActivityRemarkService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ActivityRemarkServiceImpl implements ActivityRemarkService {

    @Resource
    private ActivityRemarkMapper activityRemarkMapper;

    /**
     * 添加市场活动备注
     */
    @Override
    public int saveActivityRemark(ActivityRemarkQuery activityRemarkQuery, Authentication authentication) {

        ActivityRemark activityRemark = new ActivityRemark();

        // 使用BeanUtils工具将ActivityRemarkQuery对象的属性复制到ActivityRemark对象中
        BeanUtils.copyProperties(activityRemarkQuery, activityRemark);

        // 获取当前登录用户（当前登录用户就是创建者）
        User loginUser = (User) authentication.getPrincipal();

        // 设置创建者
        activityRemark.setCreateBy(loginUser.getId());
        // 设置创建时间
        activityRemark.setCreateTime(new Date());
        // 设置删除状态（0表示正常，1表示删除）
        activityRemark.setDeleted(0);

        return activityRemarkMapper.insertSelective(activityRemark);
    }

    /**
     * 根据市场活动id查询市场活动备注（分页查询）
     */
    @Override
    public PageInfo<ActivityRemark> getActivityRemarkListByActivityId(Integer current, Integer activityId) {

        // 1、设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);

        // 2、查询
        List<ActivityRemark> activityRemarkList = activityRemarkMapper.selectActivityRemarkListByActivityId(activityId);

        // 3、封装分页数据到PageInfo
        PageInfo<ActivityRemark> activityRemarkPageInfo = new PageInfo<>(activityRemarkList);

        return activityRemarkPageInfo;
    }

    /**
     * 编辑备注
     */
    @Override
    public int editActivityRemark(ActivityRemarkQuery activityRemarkQuery, Authentication authentication) {

        ActivityRemark activityRemark = new ActivityRemark();

        // 使用BeanUtils将ActivityRemarkQuery对象的属性复制到ActivityRemark对象
        BeanUtils.copyProperties(activityRemarkQuery, activityRemark);

        // 编辑者就是当前登录用户，通过认证信息获取当前登录用户
        User loginUser = (User) authentication.getPrincipal();

        // 设置编辑者
        activityRemark.setEditBy(loginUser.getId());
        // 设置编辑时间
        activityRemark.setEditTime(new Date());

        return activityRemarkMapper.updateByPrimaryKeySelective(activityRemark);
    }

    /**
     * 删除备注
     */
    @Override
    public int removeActivityRemarkById(Integer id) {

        // 逻辑删除：不删数据，只是修改一下数据的状态，数据依然还在表中
        // 物理删除：真正的把数据从数据库表中删掉

        // 根据id修改备注的数据状态（修改deleted字段，0表示正常，1表示删除）
        ActivityRemark activityRemark = new ActivityRemark();
        activityRemark.setId(id);
        activityRemark.setDeleted(1);

        // 保存修改后的数据状态（逻辑删除）
        return activityRemarkMapper.updateByPrimaryKeySelective(activityRemark);
    }

}

